package com.example.taximeter.controller;


import com.example.taximeter.bean.*;
import com.example.taximeter.service.FactService;
import com.example.taximeter.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 计价器厂商
 */
@RestController
@RequestMapping(value = "/fact")
public class FactController {

    private FactService factService;
    private UserService userService;
//    private final static Logger logger = LoggerFactory.getLogger(ParamFileController.class);


    @Autowired
    public FactController(FactService factService,UserService userService){
        this.factService = factService;
        this.userService = userService;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Fact> findFactList(){
        return factService.findFactList();
    }

    //查看计价器厂商列表
    @RequestMapping(value = "/all",method = RequestMethod.POST)
    public ReturnInfo findFactAll(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                       @RequestBody Fact fact) {
        //添加日志
        userService.addUserLog(7,fact.toString());
        return factService.findFactAll(Integer.valueOf(pageNum), Integer.valueOf(pageSize), fact);
    }

    //新增计价器厂商
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ReturnInfo addFact(@RequestBody  @Validated(GroupCreate.class) Fact fact){
        String mes = factService.addFact(fact);
        if(mes.equals("成功")){
//            logger.info("新增计价器厂商"+fact.toString());
            //添加日志
            userService.addUserLog(4,fact.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //修改计价器厂商
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ReturnInfo updateFact(@RequestBody  @Validated(GroupUpdate.class) Fact fact){
        String mes = factService.updateFact(fact);
        if(mes.equals("成功")){
//            logger.info("修改计价器厂商"+fact.toString());
            //添加日志
            userService.addUserLog(5,fact.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //删除计价器厂商
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public ReturnInfo deleteFact(@PathVariable Integer id){

        String mes = factService.deleteFact(id);
        if(mes.equals("成功")){
//            logger.info("删除计价器厂商"+String.valueOf(id));
            //添加日志
            userService.addUserLog(6,String.valueOf(id));
        }
        return ReturnInfo.info1(mes);
    }
}
